package cc.mrbird.febs.utils;

import cc.mrbird.febs.business.entity.Business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeUtils {

    /*
     * @Author HBH
     * @Description 返回map,key：id， value：业务全名
     * @Date 2022/7/5
     **/
    public static Map<Long, String> getFullNameById(List<Business> list) {
        Map<Long, String> map = new HashMap<>();
        for (Business business : list) {
            if (business.getFatherId().equals(0L)) {
                map.put(business.getId(), business.getName());
            } else {
                map.put(business.getId(), map.get(business.getFatherId()) + "-" + business.getName());
            }
        }
        return map;
    }

    /*
     * @Author HBH
     * @Description 返回map,key：code， value：业务全名
     * @Date 2022/7/5
     **/
    public static Map<String, String> getFullNameByCode(List<Business> list) {
        Map<String, String> map = new HashMap<>();
        Map<Long, String> codeMap = new HashMap<>();
        for (Business business : list) {
            codeMap.put(business.getId(), business.getCode());
        }
        for (Business business : list) {
            if (business.getFatherId().equals(0L)) {
                map.put(codeMap.get(business.getId()), business.getName());
            } else {
                map.put(codeMap.get(business.getId()), map.get(codeMap.get(business.getFatherId())) + "-" + business.getName());
            }
        }
        return map;
    }

    public static Map<String, String> getFatherNode(List<Business> list) {
        Map<Long, Long> map1 = new HashMap<>();  // id 和对应的fatherId
        Map<Long, String> map3 = new HashMap<>();    // id 和idCode
        Map<String, String> map4 = new HashMap<>();
        for (Business business : list) {
            map1.put(business.getId(), business.getFatherId());
            map3.put(business.getId(), business.getCode());
        }
        Long t;
        for (Business business : list) {
            if(map1.get(business.getId())!=0) {
              t = business.getId();
                do {
                    t = map1.get(t);
                } while (map1.get(t) != 0);
              map4.put(map3.get(business.getId()), map3.get(t));
            } else{
                map4.put(map3.get(business.getId()),map3.get(business.getId()));
            }
        }
        return map4;
    }
}
